package second.codeTop.package1;

/**
 * @ClassName LC200
 * @description: 岛屿数量
 * @author: 荣燊
 * @create: 2022-03-04 13:57
 **/
public class LC200 {
    public static void main(String[] args) {

    }

    public static int numIslands(char[][] grid) {
        int count = 0;
        for (int i=0; i<grid.length; i++) {
            for (int j=0; j<grid[0].length; j++) {
                if (grid[i][j] == '1') {
                    dfs(grid, i, j);
                    count++;
                }
            }
        }
        return count;
    }

    // 将每次遍历过的标记为一个不同的数
    public static void dfs(char[][] grid, int x, int y) {
        if (x < 0 || x >= grid.length ||
            y < 0 || y >= grid[0].length || grid[x][y] != '1') {
            return;
        }
        grid[x][y] = '2';
        dfs(grid, x-1, y);
        dfs(grid, x+1, y);
        dfs(grid, x, y-1);
        dfs(grid, x, y+1);
    }
}
